<?php

declare(strict_types=1);

namespace app\api\controller\v1;


use think\facade\Request;
use app\api\service\StatusCode;
use app\api\service\UserToken;
use app\api\validate\ParamMustBePostiveInt;
use app\api\model\Stage as ModelStage;
use app\api\validate\Stage as ValidateStage;
use app\api\model\Course as ModelCourse;

/**
 * 全站的公共资源数据
 */
class Stage extends Api
{
    /**
     * 获取课程一阶段周期
     */
    public function index()
    {
        $course_id = (new ParamMustBePostiveInt('course_id'))->paramCheck();
        $stage_id = (new ParamMustBePostiveInt('stage_id'))->paramCheck();
        $data = ModelStage::getFirstStageWeek($course_id, $stage_id);
        if ($data === false) {
            $this->return_msg(StatusCode::NOTFOUND, '数据获取失败', false);
        }
        if ($data === 0) {
            exit(json_encode([
                'code' => 200,
                'message' => '数据获取成功',
                'data' => []
            ]));
        }
        exit(json_encode([
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $data
        ]));
    }

    /**
     * 
     * 增加阶段
     */

    public function create()
    {
        $course_id = (new ParamMustBePostiveInt('course_id'))->paramCheck();
        $user_id = UserToken::getCurrentUid();
        $data = ModelStage::addStage($course_id, $user_id);
        if (empty($data->toArray())) {
            exit(json_encode([
                'code' => 400,
                'message' => '新增失败'
            ]));
        }
        exit(json_encode([
            'code' => 200,
            'message' => '新增成功',
            'data' => $data
        ]));
    }

    /**
     * 
     * 获取单个阶段下课程
     */
    public function read()
    {
        $stage_id = (new ParamMustBePostiveInt('stage_id'))->paramCheck();
        $data = ModelStage::getOnlyCourse($stage_id);
        if(empty($data->toArray())){
            exit(json_encode([
                'code' => 400,
                'message' => '获取信息失败'
            ]));
        }
        exit(json_encode([
            'code' => 200,
            'message' => '数据获取成功',
            'data' => $data
        ]));
    }
}
